package com.example.wechat.util;

import java.security.MessageDigest;

public  class autographUtil {
    public static String encrypt(String appKey,long timeStamp,long randomNum){
        byte[] k = appKey.getBytes();
        byte[] t = getBytesByLong(timeStamp);
        byte[] n = getBytesByLong(randomNum);
        byte[] np = new byte[k.length+t.length+n.length];
        int i=0;
        for(int j=0;j<k.length;j++,i++){
            np[i]=k[j];
        }
        for(int j=0;j<t.length;j++,i++){
            np[i]=t[j];
        }
        for(int j=0;j<n.length;j++,i++){
            np[i]=n[j];
        }
        String sha1 = getSHA1(np);
        return sha1;
    }

    public static byte[] getBytesByLong(long data){
        byte[] bytes = new byte[8];
        bytes[0] = (byte) (data & 0xff);
        bytes[1] = (byte) ((data >> 8) & 0xff);
        bytes[2] = (byte) ((data >> 16) & 0xff);
        bytes[3] = (byte) ((data >> 24) & 0xff);
        bytes[4] = (byte) ((data >> 32) & 0xff);
        bytes[5] = (byte) ((data >> 40) & 0xff);
        bytes[6] = (byte) ((data >> 48) & 0xff);
        bytes[7] = (byte) ((data >> 56) & 0xff);
        return bytes;
    }

    public static String getSHA1(byte[] str) {
        String sha1 = "";

        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(str);

            byte[] messageDigest = digest.digest();
            StringBuilder hexString = new StringBuilder();
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }

            sha1 = hexString.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sha1.toUpperCase();
    }

    public static void main(String[] args) {
        //获取sig
        Long awd = DateUtils.getTimeStamp();
        System.out.println(awd);
        String x  = autographUtil.encrypt("d73HOm5eu9yH",1605676110028L,97805);
        System.out.println(x);
    }

}
